#include <stdio.h>
#include "ddas_datarow.h"
#include <string.h>
#include <iostream>
#include "ddas_definitions.h"
#include "ddas_validator_helpers.h"

int main( int argc, char* argv[] )
{
using namespace std;

char xml[]={"<UNCWDDASResultSet><dataRow defNumber=100 SSE=0.052><coef id=0 value=0.7561/><coef id=1 value=0.9881/><coef id=2 value = 567.8941/><coef id=7 value= 4.5689/></dataRow><dataRow defNumber=11467 SSE 456.34><coef id=0 value=5.6782/><coef id=1 value=3.7891/><coef id=2 value = 8.6784/><coef id=7 value= 5.3895/></dataRow></UNCWDDASResultSet>"};

FILE* fp=fopen("./test.xml","r");
//printf("Size of file (including NULL) is: %i\n", getFileSize(fp));
puts("FILE CONTENT IS:");
puts(filePointerToCharPointer(fp));
puts(filePointerToCharPointer(fp));

char* defNums[2];
getDefNumbers(defNums, filePointerToCharPointer(fp));

for(int i=0; i<2; i++)
{
	puts(defNums[i]);
}

}
/*
int getDefNumbers(char* defNums[], char xml[])
{
	char* space=" ";
	char* remainingXML=(char*)malloc(strlen(xml));
	char* curString=(char*)malloc(strlen(xml));
	int index=0;

	while((remainingXML=strstr(xml,"defNumber="))!=NULL)
	{
		strcpy(xml,remainingXML+strlen("defNumber="));
		curString=mystrtok(xml,space);
		defNums[index++]=curString;
		//puts(curString);
		//puts(xml);
	}

return 1;
}

char* mystrtok(char* string, char* delim)
{
	int strLen=strlen(string);
	char* result=(char*)malloc(strlen(string));
	char* space=" ";

	for(int i=0; i<strLen; i++)
	{
		if(string[i]==*space)
		{
			//puts("MATCH FOUND");
			return result;
		}
		
		result[i]=string[i];
	}	

	//puts("NO MATCHES");
	return "NO MATCHES";
}

int getFileSize(FILE* fp)
{
	int bytesRead=0;
	while(fgetc(fp)!=EOF)
	{
		bytesRead++;
	}

	rewind(fp);
	return bytesRead;
}

char* filePointerToCharPointer(FILE* fp)
{
	char* fileData=(char*)malloc(getFileSize(fp));
	int index=0;
	int c;
	
	while((c=fgetc(fp))!=EOF)
	{
		fileData[index++]=c;
	}	

	rewind(fp);
	return fileData;
} 
*/
/*int one()
{
	DDAS_DataRow rows [10];
	
	for(int i=0; i<10; i++)
	{
		rows[i].defNumber=i;
		rows[i].sse=(double)i;

		//printf("Row %i has values: %i and %f\n\n",i, rows[i].defNumber, rows[i].sse);
	}

	two((void*) &rows);

	return 0;
}

int two(void* data)
{
	int DATAROW_COUNT=10;
	DDAS_DataRow *rows1=(DDAS_DataRow*)data;
	//DDAS_DataRow *rows2=(DDAS_DataRow*)data;
	DDAS_DataRow rows2 [10];	

       	for(int i=0; i<10; i++)
        {
                //rows1[i].defNumber=i;
		rows2[i].defNumber=i+1;
                //rows1[i].sse=(double)i;
		//rows2[i].defNumber=i;

                //printf("In two row %i has values: %i and %f\n\n",i, rowsInTwo[i].defNumber, rowsInTwo[i].sse);
        }

	bool match=true;
        for(int i=0; i<DATAROW_COUNT; i++)
        {
                for(int j=0; j<DATAROW_COUNT; j++)
                {
			printf("Comparing %i and %i\n", rows1[i].defNumber, rows2[j].defNumber);
                        if(rows1[i].defNumber == rows2[j].defNumber) break;

                        if(j==DATAROW_COUNT-1)
                        {
				printf("The validation result is not a match!");
				return 0;
                        }
                }
        }   

	printf("The validation result is a match!");

	return 0;
}*/
